﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Ext.h".
//-------------------------------------------------------------------

#ifndef _UXS_GL_VIEWPORT_H
#define _UXS_GL_VIEWPORT_H

#pragma once

#include "../shell/GLCommon.h"
#include "GLMatrixStack.h"

namespace UX_STL
{
namespace GLShell
{

	//
	// Viewport 2D
	//

	class UX_EXPORT GLViewport : public GLBaseObject
	{
	protected:
		irect					_sViewport;
		float					_fAspect;			// height / width
		frect					_sViewRect;
		fmat4					_sOrthoMatrix,
								_sFlipOrthoMatrix;
		fvec2					_vResCorrect;		// координаты гл / экранные координаты в пикселях
		fvec2					_vResCorAspect;
		bool					_bAutoCorrection;

	private:
		void _CurrectResolution();

	public:
		void _Mode2D(bool bFlipY = false);
		void _Setup2DMode(const frect &sViewRect, const fvec2 &vClipPlanes = fvec2(-1.f,1.f));

	public:
		GLViewport();
		~GLViewport();
		
		void Apply() const;
		void SetViewport(const irect &sViewport);

		const irect &	Viewport()			const	{ return _sViewport; }
		const uvec2 	Size()				const	{ return _sViewport.GetSize().To<uvec2>(); }
		float			Aspect()			const	{ return _fAspect; }

		// resolution correction //
		const fvec2 & GetResCorrect()		const	{ return _vResCorrect; }
		const fvec2 & GetResCorrectAspect()	const	{ return _vResCorAspect; }
		bool  GetAutoCorrection()			const	{ return _bAutoCorrection; }
		void  SetResCorrection(const fvec2 &vRC);
		void  SetAutoCorrection(bool bEnabled);
	};


	
/*
=================================================
	
=================================================
*/
	inline void GLViewport::Apply() const
	{
		SMAN()->Viewport( _sViewport );
	}
	
/*
=================================================
	
=================================================
*/
	inline void GLViewport::SetViewport(const irect &sViewport)
	{
		const fvec2	v_size = sViewport.GetSize().To<fvec2>();

		_fAspect	= v_size.y / v_size.x;
		_sViewport	= sViewport;
	}
		
/*
=================================================
	
=================================================
*/
	inline void GLViewport::_Mode2D(bool bFlipY)
	{
		const fmat4 &	s_mvp = bFlipY ? _sFlipOrthoMatrix : _sOrthoMatrix;
		MSTACK()->SetMatrixMVP( fmat4::Identity(), fmat4::Identity(), s_mvp, s_mvp );
	}

//-------------------------------------------------------------------

}	// GLShell
}	// UX_STL

#endif	// _UXS_GL_VIEWPORT_H